﻿using System;
using System.Runtime.InteropServices;
using System.Text;

namespace SmallERP.COM
{
    public static class IniFile
    {
        private static string _path;

        public static string FilePath
        {
            set { _path = value + "\\UserConfig.ini"; }
        }

        #region 声明读写INI文件的API函数

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal,
            StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal,
            int size, string filePath);

        #endregion

        public static void IniWriteValue(string section, string key, string iValue)
        {

            WritePrivateProfileString(section, key, iValue, _path);
        }

        public static string IniReadValue(string section, string key, string def = null)
        {
            var temp = new StringBuilder(2550);
            var i = GetPrivateProfileString(section, key, "", temp, 2550, _path);
            if (string.IsNullOrEmpty(temp.ToString())) 
            {
                if (string.IsNullOrEmpty(def))
                {
                    WritePrivateProfileString(section, key, "", _path);
                }
                else
                {
                    WritePrivateProfileString(section, key, def, _path);
                }
                temp = new StringBuilder(def);
            }
            return temp.ToString();
        }

        public static string DxfFolderPath
        {
            get
            {
                var path = IniReadValue("PinBan", "DXFFolder", "");
                return path;
            }
            set { IniWriteValue("PinBan", "DXFFolder", value); }
        }

        public static bool IsAutoAnlyseDXF
        {
            get
            {
                var va = IniReadValue("Import", "AutoDXF", "false");
                if ("1" == va)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            set
            {
                if (value)
                IniWriteValue("Import", "AutoDXF", "1");
                else
                {
                    IniWriteValue("Import", "AutoDXF", "0");
                }
            } 
        }
    }
}